import $ from '../common';
Page({
  data: {
    idx:0,
    courselist:[],
    page:1,
    tip:'上滑加载更多',
    noimg:$.noImage
  },
  // /testpaper/student/list
  onLoad(e) {
    this.setData({
      zonecode: wx.getStorageSync('addresscode') || ''
    })
    this.getuserstudent()
  },
  onShow() {
    if(this.data.isSwitch){
      this.setData({
        user:this.data.usestudent[this.data.idx],
      })
      this.reset()
    }
  },
  reset(){
    this.setData({
      page:1,
      courselist:[],
      tip:'上滑加载更多'
    })
    this.getgroups()
  },
  onReachBottom() {
    this.getgroups()
  },
  getuserstudent() { //获取_用户下认证子女
    const { zonecode} = this.data
    $.get('/user/students/certified', {
      zonecode
    }, res => {
      let user = res.students[this.data.idx]
      this.setData({
        usestudent: res.students,
        user
      })
      if(res.students.length>0)this.getgroups()
    })
  },
  switchchildren(){//去往学员选择
    $.navigateTo('/address/switchchildren')
  },
  getgroups(){//获取作业列表
    $.get('/testpaper/student/list',{
      page:this.data.page,
      limit:20,
      student_uuid:this.data.user.student_uuid
    },res=>{
      this.data.page++
      if(res.studentpaper.length<20){
        this.data.tip='已经到底了'
      }else{
        this.data.tip='上滑加载更多'
      }
      this.setData({
        courselist:[...this.data.courselist,...res.studentpaper]
      })
    })
  },
  goworkdetail(e){//去往作业详情
    const{uuid,testpaper_uuid}=e.currentTarget.dataset
    $.navigateTo(`/classroom/homeworkdetail?uuid=${uuid}&testpaper_uuid=${testpaper_uuid}`)
  },
})